package com.cleaningcloud.erp.service.impl;

import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.domain.ErpAccountsReceivable;
import com.cleaningcloud.erp.domain.vo.ErpAccountsReceivableVo;
import com.cleaningcloud.erp.mapper.*;
import com.cleaningcloud.erp.service.IErpAccountsReceivableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 应收账款Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-02
 */
@Service
public class ErpAccountsReceivableServiceImpl implements IErpAccountsReceivableService {
    @Autowired
    private ErpAccountsReceivableMapper erpAccountsReceivableMapper;

    @Autowired
    private ErpSaleMapper erpSaleMapper;

    @Autowired
    private ErpOutboundMapper erpOutboundMapper;

    @Autowired
    private ErpProcudeReturnMapper erpProcudeReturnMapper;

    @Autowired
    private ErpSaleReturnMapper erpSaleReturnMapper;

    /**
     * 查询应收账款
     *
     * @param receivableId 应收账款主键
     * @return 应收账款
     */
    @Override
    public ErpAccountsReceivable selectErpAccountsReceivableByReceivableId(Long receivableId) {
        return erpAccountsReceivableMapper.selectErpAccountsReceivableByReceivableId(receivableId);
    }

    /**
     * 查询应收账款列表
     *
     * @param erpAccountsReceivable 应收账款
     * @return 应收账款
     */
    @Override
    public List<ErpAccountsReceivableVo> selectErpAccountsReceivableList(ErpAccountsReceivable erpAccountsReceivable) {
        return erpAccountsReceivableMapper.selectErpAccountsReceivableList(erpAccountsReceivable);
    }

    /**
     * 新增应收账款
     *
     * @param erpAccountsReceivable 应收账款
     * @return 结果
     */
    @Override
    public int insertErpAccountsReceivable(ErpAccountsReceivable erpAccountsReceivable) {
        return erpAccountsReceivableMapper.insertErpAccountsReceivable(erpAccountsReceivable);
    }

    /**
     * 修改应收账款
     *
     * @param erpAccountsReceivable 应收账款
     * @return 结果
     */
    @Override
    public int updateErpAccountsReceivable(ErpAccountsReceivable erpAccountsReceivable) {
        return erpAccountsReceivableMapper.updateErpAccountsReceivable(erpAccountsReceivable);
    }

    /**
     * 批量删除应收账款
     *
     * @param receivableIds 需要删除的应收账款主键
     * @return 结果
     */
    @Override
    public int deleteErpAccountsReceivableByReceivableIds(Long[] receivableIds) {
        return erpAccountsReceivableMapper.deleteErpAccountsReceivableByReceivableIds(receivableIds);
    }

    /**
     * 删除应收账款信息
     *
     * @param receivableId 应收账款主键
     * @return 结果
     */
    @Override
    public int deleteErpAccountsReceivableByReceivableId(Long receivableId) {
        return erpAccountsReceivableMapper.deleteErpAccountsReceivableByReceivableId(receivableId);
    }

    @Override
    @Transactional
    public int receivingPaymentsErpAccountsPayable(ErpAccountsReceivable erpAccountsReceivable) {
        erpAccountsReceivable.setOddMoney(erpAccountsReceivable.getOddSumMoney());
        erpAccountsReceivable.setOddStatus(1);
        erpAccountsReceivable.setUserId(SecurityUtils.getUserId());
        erpAccountsReceivable.setUserName(SecurityUtils.getUsername());
        erpAccountsReceivable.setPayTime(new Date());
//        // 销售出库单
//        ErpOutbound erpOutbound = new ErpOutbound();
//        // 销售出库明细
//        List<ErpOutboundDetails> erpOutboundDetailsList = new ArrayList<>();
//        // 生成销售出库单数据
//        if (erpAccountsReceivable.getOddType() == 1) {
//            ErpSaleVo erpSaleVo = erpSaleMapper.getSaleBySaleNo(erpAccountsReceivable.getOddNo());
//            Optional.ofNullable(erpSaleVo).orElseThrow(() -> new RuntimeException("销售单查询失败！"));
//            // 计算应出库总量
//            int answerScheduledReceipt = (int) erpSaleVo.getDetailsList()
//                    .stream()
//                    .mapToDouble(ErpSaleDetails::getSaleNumber)
//                    .sum();
//            // 销售出库单字段赋值
//            erpOutbound = setOutboundFields(erpAccountsReceivable, erpSaleVo.getWareId(), answerScheduledReceipt);
//            erpOutboundMapper.insertErpOutbound(erpOutbound);
//            // 销售单明细
//            erpOutboundDetailsList = getErpOutboundDetails(erpSaleVo.getDetailsList(), erpOutbound);
//        } else if(erpAccountsReceivable.getOddType() == 2) {
//            // 采购退货单
//            ErpProcudeReturnVo erpProcudeReturnVo = erpProcudeReturnMapper.selectErpProcudeReturnByProcudeReturnOdd(erpAccountsReceivable.getOddNo());
//            Optional.ofNullable(erpProcudeReturnVo).orElseThrow(() -> new RuntimeException("采购单查询失败！"));
//            // 过滤出采购退换明细中的退换货商品
//            List<ErpReturnDetailsVo> erpReturnDetailsList = erpProcudeReturnVo.getDetailsList().stream()
//                    .filter(n -> n.getReturnCommodityType() == 1).collect(Collectors.toList());
//            erpProcudeReturnVo.setDetailsList(erpReturnDetailsList);
//            // 计算应出库总量
//            int answerScheduledReceipt = (int) erpReturnDetailsList
//                    .stream()
//                    .mapToDouble(ErpReturnDetails::getCommodityNumber)
//                    .sum();
//            // 取出仓库id 退换货类型为采购单时 因商品入库都是同一个库 则默认取出第一条仓库id即刻
//            Long wareId = erpReturnDetailsList.stream().findAny().map(ErpReturnDetailsVo::getWareId).orElse(null);
//            // 销售出库单字段赋值
//            erpOutbound = setOutboundFields(erpAccountsReceivable, wareId, answerScheduledReceipt);
//            erpOutboundMapper.insertErpOutbound(erpOutbound);
//            // 销售单明细
//            erpOutboundDetailsList = getErpOutboundDetailsByProcudeReturn(erpReturnDetailsList, erpOutbound);
//        } else if( erpAccountsReceivable.getOddType() == 3){
//            // 销售退换货单
//            ErpSaleReturnVo erpSaleReturnVo = erpSaleReturnMapper.selectErpSaleReturnBySaleReturnOdd(erpAccountsReceivable.getOddNo());
//            Optional.ofNullable(erpSaleReturnVo).orElseThrow(() -> new RuntimeException("销售单查询失败！"));
//            // 过滤出采购退换明细中的退换货商品
//            List<ErpReturnDetailsVo> erpReturnDetailsList = erpSaleReturnVo.getDetailsList().stream()
//                    .filter(n -> n.getReturnCommodityType() == 2).collect(Collectors.toList());
//            erpSaleReturnVo.setDetailsList(erpReturnDetailsList);
//            // 计算应出库总量
//            int answerScheduledReceipt = (int) erpReturnDetailsList
//                    .stream()
//                    .mapToDouble(ErpReturnDetails::getCommodityNumber)
//                    .sum();
//            erpOutbound = setOutboundFields(erpAccountsReceivable, erpSaleReturnVo.getWareId(), answerScheduledReceipt);
//            erpOutboundMapper.insertErpOutbound(erpOutbound);
//            // 销售单明细
//            erpOutboundDetailsList = getErpOutboundDetailsByProcudeReturn(erpReturnDetailsList, erpOutbound);
//        }
//        if (erpOutboundDetailsList.size() > 0) {
//            erpOutboundMapper.batchOutboundDetailsInsert(erpOutboundDetailsList);
//        }
        return erpAccountsReceivableMapper.updateErpAccountsReceivable(erpAccountsReceivable);
    }

    @Override
    public int invoicingErpAccountsPayable(ErpAccountsReceivable erpAccountsReceivable) {
        // 计算开票金额不能大于应收金额
        if (erpAccountsReceivable.getOddSumMoney().compareTo(erpAccountsReceivable.getInvoicingMoney()) < 0) {
            throw new RuntimeException("开票金额不能大于应收金额");
        }
        return erpAccountsReceivableMapper.updateErpAccountsReceivable(erpAccountsReceivable);
    }

    @Override
    public Map<String, Objects> statistics() {
        return erpAccountsReceivableMapper.statistics();
    }


}
